<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="OpenCV图像处理基础, study">
    <meta name="description" content="">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>OpenCV图像处理基础 | 今夕清欢</title>

    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/artitalk/artitalk.min.css">
    <link rel="icon" type="image/png" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/favicon.png">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/css/matery.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/css/my.css">

    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="今夕清欢" type="application/atom+xml">
<link rel="stylesheet" href="/css/prism-hopscotch.css" type="text/css"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">今夕清欢</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/artitalk" class="waves-effect waves-light">
      
      <i class="fas fa-heartbeat" style="zoom: 0.6;"></i>
      
      <span>说说</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">今夕清欢</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/artitalk" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-heartbeat"></i>
			
			ArtiTalk
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/jinxiqinghuan" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/jinxiqinghuan" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    
<script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/medias/featureimages/2.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">OpenCV图像处理基础</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86/">
                                <span class="chip bg-color">图像处理</span>
                            </a>
                        
                            <a href="/tags/OpenCV/">
                                <span class="chip bg-color">OpenCV</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86/" class="post-category">
                                图像处理
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-12-23
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2020-10-21
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    2.1k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    11 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h5 id="图片的读取"><a href="#图片的读取" class="headerlink" title="图片的读取"></a>图片的读取</h5><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Package Imported"</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">#读取图片</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"D:/image/lena.jpg"</span><span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Output"</span><span class="token punctuation">,</span> img<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>destroyAllWindows<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h4 id="视频读取和开启摄像头"><a href="#视频读取和开启摄像头" class="headerlink" title="视频读取和开启摄像头"></a>视频读取和开启摄像头</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2

<span class="token comment" spellcheck="true"># #打开视频文件</span>
<span class="token comment" spellcheck="true"># cap = cv2.VideoCapture("D:/image/music.mp4")</span>
<span class="token comment" spellcheck="true">#</span>
<span class="token comment" spellcheck="true"># while True:</span>
<span class="token comment" spellcheck="true">#     success, img = cap.read()</span>
<span class="token comment" spellcheck="true">#     cv2.imshow("Video", img)</span>
<span class="token comment" spellcheck="true">#     if cv2.waitKey(1) &amp; 0xFF == ord('q'):</span>
<span class="token comment" spellcheck="true">#         break</span>


<span class="token comment" spellcheck="true"># 摄像头</span>
cap <span class="token operator">=</span> cv2<span class="token punctuation">.</span>VideoCapture<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
cap<span class="token punctuation">.</span>set<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">640</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 高度</span>
cap<span class="token punctuation">.</span>set<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">480</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 宽度</span>
cap<span class="token punctuation">.</span>set<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">#亮度</span>

<span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
    sucess<span class="token punctuation">,</span> img <span class="token operator">=</span> cap<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
    cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Video"</span><span class="token punctuation">,</span> img<span class="token punctuation">)</span>
    <span class="token keyword">if</span> cv2<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token number">0xFF</span> <span class="token operator">==</span> ord<span class="token punctuation">(</span><span class="token string">'q'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">break</span></code></pre>
<h4 id="图像的基本处理"><a href="#图像的基本处理" class="headerlink" title="图像的基本处理"></a>图像的基本处理</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

kernel <span class="token operator">=</span> np<span class="token punctuation">.</span>ones<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span>np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">#print(kernel)</span>

<span class="token comment" spellcheck="true"># 原始图片</span>
img <span class="token operator">=</span> cv<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"D:/image/lena.jpg"</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Original Image"</span><span class="token punctuation">,</span> img<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 灰度图</span>
imgGray <span class="token operator">=</span> cv<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span> cv<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Gray Image"</span><span class="token punctuation">,</span> imgGray<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 高斯模糊</span>
imgBlur <span class="token operator">=</span> cv<span class="token punctuation">.</span>GaussianBlur<span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Blur Image"</span><span class="token punctuation">,</span>imgBlur<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 边缘提取</span>
imgCanny <span class="token operator">=</span> cv<span class="token punctuation">.</span>Canny<span class="token punctuation">(</span>img<span class="token punctuation">,</span><span class="token number">150</span><span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Canny Image"</span><span class="token punctuation">,</span>imgCanny<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># Dialation</span>
imgDialation <span class="token operator">=</span> cv<span class="token punctuation">.</span>dilate<span class="token punctuation">(</span>imgCanny<span class="token punctuation">,</span>kernel<span class="token punctuation">,</span>iterations<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Dilation Image"</span><span class="token punctuation">,</span>imgDialation<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># Eroded</span>
imgEroded <span class="token operator">=</span> cv<span class="token punctuation">.</span>erode<span class="token punctuation">(</span>imgDialation<span class="token punctuation">,</span>kernel<span class="token punctuation">,</span>iterations<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Eroded Image"</span><span class="token punctuation">,</span> imgEroded<span class="token punctuation">)</span>

cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span></code></pre>
<h4 id="图像缩小与剪裁"><a href="#图像缩小与剪裁" class="headerlink" title="图像缩小与剪裁"></a>图像缩小与剪裁</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

img <span class="token operator">=</span> cv<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"D:/image/myPicture/Maugham.jpg"</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Image"</span><span class="token punctuation">,</span> img<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># cv.waitKey(0) #如果这里也有waitKey的话</span>
<span class="token comment" spellcheck="true"># 等待关闭这张图片再运行下面的程序</span>

<span class="token comment" spellcheck="true"># 重定义图片大小</span>
imgResize <span class="token operator">=</span> cv<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">,</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Image Resize"</span><span class="token punctuation">,</span> imgResize<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>imgResize<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 切割</span>
imgCropped <span class="token operator">=</span> img<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">200</span><span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">:</span><span class="token number">500</span><span class="token punctuation">]</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Image Cropped"</span><span class="token punctuation">,</span> imgCropped<span class="token punctuation">)</span>

cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
</code></pre>
<h4 id="OpenCV绘图"><a href="#OpenCV绘图" class="headerlink" title="OpenCV绘图"></a>OpenCV绘图</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

<span class="token comment" spellcheck="true"># 设置画布</span>
img <span class="token operator">=</span> np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">512</span><span class="token punctuation">,</span><span class="token number">512</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span>np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># img[200:300,100:300] = 255,0,0</span>

<span class="token comment" spellcheck="true"># 线</span>
cv<span class="token punctuation">.</span>line<span class="token punctuation">(</span>img<span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 矩形</span>
cv<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>img<span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">250</span><span class="token punctuation">,</span><span class="token number">350</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>cv<span class="token punctuation">.</span>FILLED<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># Circle</span>
cv<span class="token punctuation">.</span>circle<span class="token punctuation">(</span>img<span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># Text</span>
cv<span class="token punctuation">.</span>putText<span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token string">"OPENCV"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span><span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">,</span>cv<span class="token punctuation">.</span>FONT_HERSHEY_COMPLEX<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">150</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Test"</span><span class="token punctuation">,</span>img<span class="token punctuation">)</span>


cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span></code></pre>
<h4 id="透视变换"><a href="#透视变换" class="headerlink" title="透视变换"></a>透视变换</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

<span class="token comment" spellcheck="true"># 透视变换</span>
img <span class="token operator">=</span> cv<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"D:/image/cameraman.tif"</span><span class="token punctuation">)</span>

width<span class="token punctuation">,</span>height <span class="token operator">=</span> <span class="token number">250</span><span class="token punctuation">,</span><span class="token number">350</span>
pst1 <span class="token operator">=</span> np<span class="token punctuation">.</span>float32<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">111</span><span class="token punctuation">,</span><span class="token number">219</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">287</span><span class="token punctuation">,</span><span class="token number">188</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">154</span><span class="token punctuation">,</span><span class="token number">482</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">352</span><span class="token punctuation">,</span><span class="token number">440</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
pst2 <span class="token operator">=</span> np<span class="token punctuation">.</span>float32<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span>width<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span>height<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span>width<span class="token punctuation">,</span>height<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
matrix <span class="token operator">=</span> cv<span class="token punctuation">.</span>getPerspectiveTransform<span class="token punctuation">(</span>pst1<span class="token punctuation">,</span>pst2<span class="token punctuation">)</span>

imgOutput <span class="token operator">=</span> cv<span class="token punctuation">.</span>warpPerspective<span class="token punctuation">(</span>img<span class="token punctuation">,</span>matrix<span class="token punctuation">,</span><span class="token punctuation">(</span>width<span class="token punctuation">,</span>height<span class="token punctuation">)</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"IMG Output"</span><span class="token punctuation">,</span>imgOutput<span class="token punctuation">)</span>

cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"test"</span><span class="token punctuation">,</span>img<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h4 id="图像堆叠排放"><a href="#图像堆叠排放" class="headerlink" title="图像堆叠排放"></a>图像堆叠排放</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

img1 <span class="token operator">=</span> cv<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"D:/image/myPicture/lena.jpg"</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">#img2 = cv.imread("D:/image/myPicture/Maugham.jpg")</span>

<span class="token comment" spellcheck="true"># why can not add img2?</span>
<span class="token comment" spellcheck="true"># 因为维度不一样吗？应该是</span>
<span class="token comment" spellcheck="true"># so，如何解决？</span>
imgHor <span class="token operator">=</span> np<span class="token punctuation">.</span>hstack<span class="token punctuation">(</span><span class="token punctuation">(</span>img1<span class="token punctuation">,</span> img1<span class="token punctuation">)</span><span class="token punctuation">)</span>
imgVer <span class="token operator">=</span> np<span class="token punctuation">.</span>vstack<span class="token punctuation">(</span><span class="token punctuation">(</span>img1<span class="token punctuation">,</span>img1<span class="token punctuation">)</span><span class="token punctuation">)</span>

cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Horizontal"</span><span class="token punctuation">,</span> imgHor<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Vertical"</span><span class="token punctuation">,</span> imgVer<span class="token punctuation">)</span>

cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span></code></pre>
<p>多照片堆叠;</p>
<pre class=" language-python"><code class="language-python">
<span class="token comment" spellcheck="true"># ~ 本文件是为了把多张照片拼凑到一起 </span>

<span class="token comment" spellcheck="true"># ^ 设置几张图片拼接</span>
<span class="token keyword">def</span> <span class="token function">stackImages</span><span class="token punctuation">(</span>scale<span class="token punctuation">,</span>imgArray<span class="token punctuation">)</span><span class="token punctuation">:</span>
    rows <span class="token operator">=</span> len<span class="token punctuation">(</span>imgArray<span class="token punctuation">)</span>
    cols <span class="token operator">=</span> len<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># &amp; 输出一个 rows * cols 的矩阵（imgArray）</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>rows<span class="token punctuation">,</span>cols<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># &amp; 判断imgArray[0] 是不是一个list</span>
    rowsAvailable <span class="token operator">=</span> isinstance<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> list<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># &amp; imgArray[][] 是什么意思呢？</span>
    <span class="token comment" spellcheck="true"># &amp; imgArray[0][0]就是指[0,0]的那个图片（我们把图片集分为二维矩阵，第一行、第一列的那个就是第一个图片）</span>
    <span class="token comment" spellcheck="true"># &amp; 而shape[1]就是width，shape[0]是height，shape[2]是</span>
    width <span class="token operator">=</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
    height <span class="token operator">=</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

    <span class="token comment" spellcheck="true"># &amp; 例如，我们可以展示一下是什么含义</span>
    cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"img"</span><span class="token punctuation">,</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

    <span class="token keyword">if</span> rowsAvailable<span class="token punctuation">:</span>
        <span class="token keyword">for</span> x <span class="token keyword">in</span> range <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> y <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> cols<span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token comment" spellcheck="true"># &amp; 判断图像与后面那个图像的形状是否一致，若一致则进行等比例放缩；否则，先resize为一致，后进行放缩</span>
                <span class="token keyword">if</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">==</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span> scale<span class="token punctuation">,</span> scale<span class="token punctuation">)</span>
                <span class="token keyword">else</span><span class="token punctuation">:</span>
                    imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span> scale<span class="token punctuation">,</span> scale<span class="token punctuation">)</span>
                <span class="token comment" spellcheck="true"># &amp; 如果是灰度图，则变成RGB图像（为了弄成一样的图像）</span>
                <span class="token keyword">if</span> len<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">:</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_GRAY2BGR<span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># &amp; 设置零矩阵</span>
        imageBlank <span class="token operator">=</span> np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>height<span class="token punctuation">,</span> width<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span>
        hor <span class="token operator">=</span> <span class="token punctuation">[</span>imageBlank<span class="token punctuation">]</span><span class="token operator">*</span>rows
        hor_con <span class="token operator">=</span> <span class="token punctuation">[</span>imageBlank<span class="token punctuation">]</span><span class="token operator">*</span>rows
        <span class="token keyword">for</span> x <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            hor<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> np<span class="token punctuation">.</span>hstack<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
        ver <span class="token operator">=</span> np<span class="token punctuation">.</span>vstack<span class="token punctuation">(</span>hor<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># &amp; 如果不是一组照片，则仅仅进行放缩 or 灰度转化为RGB</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">for</span> x <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">==</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span> scale<span class="token punctuation">,</span> scale<span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span>scale<span class="token punctuation">,</span> scale<span class="token punctuation">)</span>
            <span class="token keyword">if</span> len<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">:</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_GRAY2BGR<span class="token punctuation">)</span>
        hor<span class="token operator">=</span> np<span class="token punctuation">.</span>hstack<span class="token punctuation">(</span>imgArray<span class="token punctuation">)</span>
        ver <span class="token operator">=</span> hor
    <span class="token keyword">return</span> ver



<span class="token keyword">import</span> cv2
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np


<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"Resources/lena.png"</span><span class="token punctuation">)</span>
    imgGray <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_RGB2GRAY<span class="token punctuation">)</span>
    imgFilter <span class="token operator">=</span> cv2<span class="token punctuation">.</span>GaussianBlur<span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
    imgStack <span class="token operator">=</span> stackImages<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token punctuation">[</span>imgGray<span class="token punctuation">,</span>img<span class="token punctuation">,</span>imgFilter<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span>imgGray<span class="token punctuation">,</span>imgGray<span class="token punctuation">,</span>imgGray<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span>imgGray<span class="token punctuation">,</span>imgGray<span class="token punctuation">,</span>imgGray<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

    cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"stackImages"</span><span class="token punctuation">,</span> imgStack<span class="token punctuation">)</span>

    cv2<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre>
<h4 id="色彩检测"><a href="#色彩检测" class="headerlink" title="色彩检测"></a>色彩检测</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

<span class="token keyword">def</span> <span class="token function">empty</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">pass</span>

path <span class="token operator">=</span> <span class="token string">"D:/image/lambo.jpg"</span>

cv<span class="token punctuation">.</span>namedWindow<span class="token punctuation">(</span><span class="token string">"TrackBars"</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>resizeWindow<span class="token punctuation">(</span><span class="token string">"TrackBars"</span><span class="token punctuation">,</span><span class="token number">640</span><span class="token punctuation">,</span><span class="token number">240</span><span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>createTrackbar<span class="token punctuation">(</span><span class="token string">"Hue Min"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">179</span><span class="token punctuation">,</span>empty<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>createTrackbar<span class="token punctuation">(</span><span class="token string">"Hue Max"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">,</span><span class="token number">179</span><span class="token punctuation">,</span>empty<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>createTrackbar<span class="token punctuation">(</span><span class="token string">"Sat Min"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">,</span><span class="token number">101</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span>empty<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>createTrackbar<span class="token punctuation">(</span><span class="token string">"Sat Max"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span>empty<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>createTrackbar<span class="token punctuation">(</span><span class="token string">"Val Min"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">,</span><span class="token number">106</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span>empty<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>createTrackbar<span class="token punctuation">(</span><span class="token string">"Val Max"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span>empty<span class="token punctuation">)</span>


<span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
    img <span class="token operator">=</span> cv<span class="token punctuation">.</span>imread<span class="token punctuation">(</span>path<span class="token punctuation">)</span>
    imgHSV <span class="token operator">=</span> cv<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span> cv<span class="token punctuation">.</span>COLOR_BGR2HSV<span class="token punctuation">)</span>
    h_min <span class="token operator">=</span> cv<span class="token punctuation">.</span>getTrackbarPos<span class="token punctuation">(</span><span class="token string">"Hue Min"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">)</span>
    h_max <span class="token operator">=</span> cv<span class="token punctuation">.</span>getTrackbarPos<span class="token punctuation">(</span><span class="token string">"Hue Max"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">)</span>
    s_min <span class="token operator">=</span> cv<span class="token punctuation">.</span>getTrackbarPos<span class="token punctuation">(</span><span class="token string">"Sat Min"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">)</span>
    s_max <span class="token operator">=</span> cv<span class="token punctuation">.</span>getTrackbarPos<span class="token punctuation">(</span><span class="token string">"Sat Max"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">)</span>
    v_min <span class="token operator">=</span> cv<span class="token punctuation">.</span>getTrackbarPos<span class="token punctuation">(</span><span class="token string">"Val Min"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">)</span>
    v_max <span class="token operator">=</span> cv<span class="token punctuation">.</span>getTrackbarPos<span class="token punctuation">(</span><span class="token string">"Val Max"</span><span class="token punctuation">,</span> <span class="token string">"TrackBars"</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>h_min<span class="token punctuation">,</span>h_max<span class="token punctuation">,</span>s_min<span class="token punctuation">,</span>s_max<span class="token punctuation">,</span>v_min<span class="token punctuation">,</span>v_max<span class="token punctuation">)</span>
    lower <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>h_min<span class="token punctuation">,</span>s_min<span class="token punctuation">,</span>v_min<span class="token punctuation">]</span><span class="token punctuation">)</span>
    upper <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>h_max<span class="token punctuation">,</span>s_max<span class="token punctuation">,</span>v_max<span class="token punctuation">]</span><span class="token punctuation">)</span>
    mask <span class="token operator">=</span> cv<span class="token punctuation">.</span>inRange<span class="token punctuation">(</span>imgHSV<span class="token punctuation">,</span>lower<span class="token punctuation">,</span>upper<span class="token punctuation">)</span>

    imgResult <span class="token operator">=</span> cv<span class="token punctuation">.</span>bitwise_and<span class="token punctuation">(</span>img<span class="token punctuation">,</span>img<span class="token punctuation">,</span>mask <span class="token operator">=</span> mask<span class="token punctuation">)</span>

    cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Result"</span><span class="token punctuation">,</span>imgResult<span class="token punctuation">)</span>
    cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Original"</span><span class="token punctuation">,</span> img<span class="token punctuation">)</span>
    cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"HSV"</span><span class="token punctuation">,</span> imgHSV<span class="token punctuation">)</span>
    cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Mask"</span><span class="token punctuation">,</span> mask<span class="token punctuation">)</span>
    cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span></code></pre>
<h4 id="形状检测"><a href="#形状检测" class="headerlink" title="形状检测"></a>形状检测</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

<span class="token comment" spellcheck="true"># ^ 设置几张图片拼接</span>
<span class="token keyword">def</span> <span class="token function">stackImages</span><span class="token punctuation">(</span>scale<span class="token punctuation">,</span>imgArray<span class="token punctuation">)</span><span class="token punctuation">:</span>
    rows <span class="token operator">=</span> len<span class="token punctuation">(</span>imgArray<span class="token punctuation">)</span>
    cols <span class="token operator">=</span> len<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># &amp; 输出一个 rows * cols 的矩阵（imgArray）</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>rows<span class="token punctuation">,</span>cols<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># &amp; 判断imgArray[0] 是不是一个list</span>
    rowsAvailable <span class="token operator">=</span> isinstance<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> list<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># &amp; imgArray[][] 是什么意思呢？</span>
    <span class="token comment" spellcheck="true"># &amp; imgArray[0][0]就是指[0,0]的那个图片（我们把图片集分为二维矩阵，第一行、第一列的那个就是第一个图片）</span>
    <span class="token comment" spellcheck="true"># &amp; 而shape[1]就是width，shape[0]是height，shape[2]是</span>
    width <span class="token operator">=</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
    height <span class="token operator">=</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

    <span class="token comment" spellcheck="true"># &amp; 例如，我们可以展示一下是什么含义</span>
    <span class="token comment" spellcheck="true">#cv.imshow("img", imgArray[0][1])</span>

    <span class="token keyword">if</span> rowsAvailable<span class="token punctuation">:</span>
        <span class="token keyword">for</span> x <span class="token keyword">in</span> range <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> y <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> cols<span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token comment" spellcheck="true"># &amp; 判断图像与后面那个图像的形状是否一致，若一致则进行等比例放缩；否则，先resize为一致，后进行放缩</span>
                <span class="token keyword">if</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">==</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> cv<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span> scale<span class="token punctuation">,</span> scale<span class="token punctuation">)</span>
                <span class="token keyword">else</span><span class="token punctuation">:</span>
                    imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> cv<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span> scale<span class="token punctuation">,</span> scale<span class="token punctuation">)</span>
                <span class="token comment" spellcheck="true"># &amp; 如果是灰度图，则变成RGB图像（为了弄成一样的图像）</span>
                <span class="token keyword">if</span> len<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">:</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">=</span> cv<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span> cv<span class="token punctuation">.</span>COLOR_GRAY2BGR<span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># &amp; 设置零矩阵</span>
        imageBlank <span class="token operator">=</span> np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>height<span class="token punctuation">,</span> width<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span>
        hor <span class="token operator">=</span> <span class="token punctuation">[</span>imageBlank<span class="token punctuation">]</span><span class="token operator">*</span>rows
        hor_con <span class="token operator">=</span> <span class="token punctuation">[</span>imageBlank<span class="token punctuation">]</span><span class="token operator">*</span>rows
        <span class="token keyword">for</span> x <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            hor<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> np<span class="token punctuation">.</span>hstack<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
        ver <span class="token operator">=</span> np<span class="token punctuation">.</span>vstack<span class="token punctuation">(</span>hor<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># &amp; 如果不是一组照片，则仅仅进行放缩 or 灰度转化为RGB</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">for</span> x <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">==</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> cv<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span> scale<span class="token punctuation">,</span> scale<span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> cv<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> imgArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span>scale<span class="token punctuation">,</span> scale<span class="token punctuation">)</span>
            <span class="token keyword">if</span> len<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span>shape<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">:</span> imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> cv<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>imgArray<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span> cv<span class="token punctuation">.</span>COLOR_GRAY2BGR<span class="token punctuation">)</span>
        hor<span class="token operator">=</span> np<span class="token punctuation">.</span>hstack<span class="token punctuation">(</span>imgArray<span class="token punctuation">)</span>
        ver <span class="token operator">=</span> hor
    <span class="token keyword">return</span> ver

<span class="token keyword">def</span> <span class="token function">getContours</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">:</span>
    contours<span class="token punctuation">,</span>hierarchy <span class="token operator">=</span> cv<span class="token punctuation">.</span>findContours<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cv<span class="token punctuation">.</span>RETR_EXTERNAL<span class="token punctuation">,</span>cv<span class="token punctuation">.</span>CHAIN_APPROX_NONE<span class="token punctuation">)</span>
    <span class="token keyword">for</span> cnt <span class="token keyword">in</span> contours<span class="token punctuation">:</span>
        area <span class="token operator">=</span> cv<span class="token punctuation">.</span>contourArea<span class="token punctuation">(</span>cnt<span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>area<span class="token punctuation">)</span>
        <span class="token keyword">if</span> area <span class="token operator">></span> <span class="token number">500</span><span class="token punctuation">:</span>
            cv<span class="token punctuation">.</span>drawContours<span class="token punctuation">(</span>imgContour<span class="token punctuation">,</span> cnt<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
            peri <span class="token operator">=</span> cv<span class="token punctuation">.</span>arcLength<span class="token punctuation">(</span>cnt<span class="token punctuation">,</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>peri<span class="token punctuation">)</span>
            approx <span class="token operator">=</span> cv<span class="token punctuation">.</span>approxPolyDP<span class="token punctuation">(</span>cnt<span class="token punctuation">,</span><span class="token number">0.02</span> <span class="token operator">*</span> peri<span class="token punctuation">,</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>len<span class="token punctuation">(</span>approx<span class="token punctuation">)</span><span class="token punctuation">)</span>
            objCor <span class="token operator">=</span> len<span class="token punctuation">(</span>approx<span class="token punctuation">)</span>
            x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> w<span class="token punctuation">,</span> h <span class="token operator">=</span> cv<span class="token punctuation">.</span>boundingRect<span class="token punctuation">(</span>approx<span class="token punctuation">)</span>

            <span class="token keyword">if</span> objCor <span class="token operator">==</span> <span class="token number">4</span><span class="token punctuation">:</span>
                objectType <span class="token operator">=</span> <span class="token string">"Rec"</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>objectType <span class="token operator">=</span> None
            cv<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>imgContour<span class="token punctuation">,</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>x <span class="token operator">+</span> w<span class="token punctuation">,</span> y <span class="token operator">+</span> h<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
            cv<span class="token punctuation">.</span>putText<span class="token punctuation">(</span>imgContour<span class="token punctuation">,</span>objectType<span class="token punctuation">,</span>
                       <span class="token punctuation">(</span>x<span class="token operator">+</span><span class="token punctuation">(</span>w<span class="token operator">//</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">10</span><span class="token punctuation">,</span> y<span class="token operator">+</span><span class="token punctuation">(</span>h<span class="token operator">//</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span>cv<span class="token punctuation">.</span>FONT_HERSHEY_COMPLEX<span class="token punctuation">,</span><span class="token number">0.7</span><span class="token punctuation">,</span>
                       <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>

path <span class="token operator">=</span> <span class="token string">"D:/image/shapes.jpg"</span>
img <span class="token operator">=</span> cv<span class="token punctuation">.</span>imread<span class="token punctuation">(</span>path<span class="token punctuation">)</span>
imgContour <span class="token operator">=</span> img<span class="token punctuation">.</span>copy<span class="token punctuation">(</span><span class="token punctuation">)</span>

imgGray <span class="token operator">=</span> cv<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cv<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
imgBlur <span class="token operator">=</span> cv<span class="token punctuation">.</span>GaussianBlur<span class="token punctuation">(</span>imgGray<span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
imgCanny <span class="token operator">=</span> cv<span class="token punctuation">.</span>Canny<span class="token punctuation">(</span>imgBlur<span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">)</span>
getContours<span class="token punctuation">(</span>imgCanny<span class="token punctuation">)</span>

imgBlank <span class="token operator">=</span> np<span class="token punctuation">.</span>zeros_like<span class="token punctuation">(</span>img<span class="token punctuation">)</span>
imgStack <span class="token operator">=</span> stackImages<span class="token punctuation">(</span><span class="token number">0.8</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token punctuation">[</span>img<span class="token punctuation">,</span>imgGray<span class="token punctuation">,</span>imgBlur<span class="token punctuation">]</span><span class="token punctuation">,</span>
                            <span class="token punctuation">[</span>imgCanny<span class="token punctuation">,</span>imgContour<span class="token punctuation">,</span>imgBlank<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Stack"</span><span class="token punctuation">,</span>imgStack<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span></code></pre>
<h4 id="脸部检测"><a href="#脸部检测" class="headerlink" title="脸部检测"></a>脸部检测</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv

faceCascade <span class="token operator">=</span> cv<span class="token punctuation">.</span>CascadeClassifier<span class="token punctuation">(</span><span class="token string">"D:\image\haarcascade_frontalface_default.xml "</span><span class="token punctuation">)</span>
img <span class="token operator">=</span> cv<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"D:\image\lena.jpg"</span><span class="token punctuation">)</span>
imgGrey <span class="token operator">=</span> cv<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cv<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>

faces <span class="token operator">=</span> faceCascade<span class="token punctuation">.</span>detectMultiScale<span class="token punctuation">(</span>img<span class="token punctuation">,</span><span class="token number">1.1</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>w<span class="token punctuation">,</span>h<span class="token punctuation">)</span> <span class="token keyword">in</span> faces<span class="token punctuation">:</span>
    cv<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>img<span class="token punctuation">,</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span>x<span class="token operator">+</span>w<span class="token punctuation">,</span>y<span class="token operator">+</span>h<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>

cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Result"</span><span class="token punctuation">,</span>img<span class="token punctuation">)</span>
cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span></code></pre>
<h4 id="文件扫描（部分）"><a href="#文件扫描（部分）" class="headerlink" title="文件扫描（部分）"></a>文件扫描（部分）</h4><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> cv2 <span class="token keyword">as</span> cv
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

<span class="token comment" spellcheck="true">##############</span>
widthImg <span class="token operator">=</span> <span class="token number">640</span>
heightImg <span class="token operator">=</span> <span class="token number">480</span>
<span class="token comment" spellcheck="true">##############</span>

<span class="token comment" spellcheck="true"># 摄像头</span>
cap <span class="token operator">=</span> cv<span class="token punctuation">.</span>VideoCapture<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
cap<span class="token punctuation">.</span>set<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">640</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 高度</span>
cap<span class="token punctuation">.</span>set<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">480</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 宽度</span>
cap<span class="token punctuation">.</span>set<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 亮度</span>

<span class="token keyword">def</span> <span class="token function">preProcessing</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">:</span>
    imgGray <span class="token operator">=</span> cv<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span> cv<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
    imgBlur <span class="token operator">=</span> cv<span class="token punctuation">.</span>GaussianBlur<span class="token punctuation">(</span>imgGray<span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
    imgGanny <span class="token operator">=</span> cv<span class="token punctuation">.</span>Canny<span class="token punctuation">(</span>imgBlur<span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">)</span>
    kernel <span class="token operator">=</span> np<span class="token punctuation">.</span>ones<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    imgDial <span class="token operator">=</span> cv<span class="token punctuation">.</span>dilate<span class="token punctuation">(</span>imgGanny<span class="token punctuation">,</span>kernel<span class="token punctuation">,</span>iterations<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
    imgThres <span class="token operator">=</span> cv<span class="token punctuation">.</span>erode<span class="token punctuation">(</span>imgDial<span class="token punctuation">,</span>kernel<span class="token punctuation">,</span>iterations<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> imgThres

<span class="token keyword">def</span> <span class="token function">getContours</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">:</span>
    biggest <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    maxArea <span class="token operator">=</span> <span class="token number">0</span>
    contours<span class="token punctuation">,</span>hierarchy <span class="token operator">=</span> cv<span class="token punctuation">.</span>findContours<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cv<span class="token punctuation">.</span>RETR_EXTERNAL<span class="token punctuation">,</span>cv<span class="token punctuation">.</span>CHAIN_APPROX_NONE<span class="token punctuation">)</span>
    <span class="token keyword">for</span> cnt <span class="token keyword">in</span> contours<span class="token punctuation">:</span>
        area <span class="token operator">=</span> cv<span class="token punctuation">.</span>contourArea<span class="token punctuation">(</span>cnt<span class="token punctuation">)</span>
        <span class="token keyword">if</span> area <span class="token operator">></span> <span class="token number">3000</span><span class="token punctuation">:</span>
            <span class="token comment" spellcheck="true">#cv.drawContours(imgContour, cnt, -1, (255, 0, 0), 3)</span>
            peri <span class="token operator">=</span> cv<span class="token punctuation">.</span>arcLength<span class="token punctuation">(</span>cnt<span class="token punctuation">,</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true">#print(peri)</span>
            approx <span class="token operator">=</span> cv<span class="token punctuation">.</span>approxPolyDP<span class="token punctuation">(</span>cnt<span class="token punctuation">,</span><span class="token number">0.02</span> <span class="token operator">*</span> peri<span class="token punctuation">,</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            <span class="token keyword">if</span> area <span class="token operator">></span> maxArea <span class="token operator">and</span> len<span class="token punctuation">(</span>approx<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">4</span><span class="token punctuation">:</span>
                biggest <span class="token operator">=</span> approx
                maxArea <span class="token operator">=</span> area
    cv<span class="token punctuation">.</span>drawContours<span class="token punctuation">(</span>imgContour<span class="token punctuation">,</span> biggest<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> biggest

<span class="token keyword">def</span> <span class="token function">getWarp</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span>biggest<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">pass</span>




<span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
    sucess<span class="token punctuation">,</span> img <span class="token operator">=</span> cap<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
    img <span class="token operator">=</span> cv<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>img<span class="token punctuation">,</span><span class="token punctuation">(</span>widthImg<span class="token punctuation">,</span> heightImg<span class="token punctuation">)</span><span class="token punctuation">)</span>
    imgContour <span class="token operator">=</span> img<span class="token punctuation">.</span>copy<span class="token punctuation">(</span><span class="token punctuation">)</span>
    imgThres <span class="token operator">=</span> preProcessing<span class="token punctuation">(</span>img<span class="token punctuation">)</span>
    biggest <span class="token operator">=</span> getContours<span class="token punctuation">(</span>imgThres<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>biggest<span class="token punctuation">)</span>
    getWarp<span class="token punctuation">(</span>img<span class="token punctuation">,</span>biggest<span class="token punctuation">)</span>

    cv<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Results"</span><span class="token punctuation">,</span> imgContour<span class="token punctuation">)</span>
    <span class="token keyword">if</span> cv<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token number">0xFF</span> <span class="token operator">==</span> ord<span class="token punctuation">(</span><span class="token string">'q'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">break</span></code></pre>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">清欢</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jinxiqinghuan.github.io/2020/12/23/opencv-tu-xiang-chu-li-ji-chu/">https://jinxiqinghuan.github.io/2020/12/23/opencv-tu-xiang-chu-li-ji-chu/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">清欢</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86/">
                                    <span class="chip bg-color">图像处理</span>
                                </a>
                            
                                <a href="/tags/OpenCV/">
                                    <span class="chip bg-color">OpenCV</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2020/12/23/pycharm-ru-men-shou-ce/">
                    <div class="card-image">
                        
                        
                        <img src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/medias/featureimages/22.jpg" class="responsive-img" alt="Pycharm入门手册">
                        
                        <span class="card-title">Pycharm入门手册</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文主要介绍pycharm的基本使用方法，并不是全面的pycharm使用教程，看完本文后能够搭建开发环境并进行简单的开发。本文以安装sklearn库为例。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-12-23
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/python/" class="post-category">
                                    python
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                    <a href="/tags/pycharm/">
                        <span class="chip bg-color">pycharm</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/12/23/ml/">
                    <div class="card-image">
                        
                        
                        <img src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/medias/featureimages/15.jpg" class="responsive-img" alt="ML">
                        
                        <span class="card-title">ML</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-12-23
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" class="post-category">
                                    机器学习
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">
                        <span class="chip bg-color">机器学习</span>
                    </a>
                    
                    <a href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">
                        <span class="chip bg-color">深度学习</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2019</span>
            <a href="/about" target="_blank">qinghuan</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">58.9k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2019";
                    var startMonth = "6";
                    var startDate = "28";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
            <span id="icp"><img src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/medias/icp.png" style="vertical-align: text-bottom;" />
                <a href="http://beian.miit.gov.cn/" target="_blank">鲁ICP备19005295号-2</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/jinxiqinghuan" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1196680037@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>





    <a href="https://twitter.com/realljt" class="tooltipped" target="_blank" data-tooltip="关注我的Twitter: https://twitter.com/realljt" data-position="top" data-delay="50">
        <i class="fab fa-twitter"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1196680037" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1196680037" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/materialize/materialize.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/aos/aos.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/background/canvas-nest.js"></script>
    

    
    
    <script type="text/javascript" size="150" alpha='0.6'
        zIndex="-1" src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/background/ribbon-refresh.min.js" async="async"></script>
    

    

    
    <script src="https://cdn.jsdelivr.net/gh/jinxiqinghuan/jinxiqinghuan.github.io/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
